package com.common.baselibrary.base;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public abstract  class BaseLazyFragment extends BaseFragment {

    private boolean isLazyDataLoaded = false;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getLazyData();
    }


    private void getLazyData() {
        if (getUserVisibleHint() && getView() != null) {
            if (!isLazyDataLoaded()) {
                lazyLoadData();
                isLazyDataLoaded = true;
            }
        }
    }

    protected void lazyLoadData(){}

    protected boolean isLazyDataLoaded(){
        return isLazyDataLoaded;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        getLazyData();
        onVisibilityChanged(isVisibleToUser);
    }


    protected void onVisibilityChanged(boolean isVisibleToUser) {
    }
}
